package com.dw.dwdzcommerce.tools.commons;

import java.sql.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Random;
import java.util.Set;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import android.content.Context;
import android.text.TextUtils;

public class StringUtil {

    /**
     * 判断是否为空
     *
     * @param input
     * @return
     */
    public static boolean isEmpty(String input) {
        if (input == null || "".equals(input) || "%null%".equals(input) || TextUtils.isEmpty(input))
            return true;
        for (int i = 0; i < input.length(); i++) {
            char c = input.charAt(i);
            if (c != ' ' && c != '\t' && c != '\r' && c != '\n') {
                return false;
            }
        }
        return true;
    }

    /**
     * 生成一段随机字符串
     *
     * @return
     */
    public static String RandomUUID() {
        String string = UUID.randomUUID().toString();
        string.replaceAll("-", "");
        string.substring(0, 5);
        return string;
    }

    public static int randomID() {
        Random r = new Random();
        return r.nextInt(1000000) + 1;
    }

    /**
     * 得到当前时间YY-MM-DD
     *
     * @param context
     * @return
     */
    public static String getTime(Context context) {
        if (context == null) {
            return null;
        }
        DateFormat format = SimpleDateFormat.getDateInstance();
        Date date = new Date(System.currentTimeMillis());
        return format.format(date);
    }

    public static String arrayToString(String[] array) {
        if (array == null || array.length <= 0 || TextUtils.isEmpty(array[0]))
            return null;
        StringBuffer buffer = new StringBuffer();
        boolean isLast = false;
        for (int i = 0, j = array.length; i < j; i++) {
            String arr = array[i];
            if (arr == null) {
                continue;
            }
            arr = arr.trim();
            buffer.append(arr);
            if (i == j - 1) {
                isLast = true;
            }
            if (!isLast) {
                buffer.append(",");
            }
        }
        return buffer.toString();
    }

    public static String[] stringToArray(String array) {
        if (array == null) {
            return null;
        }
        return array.split(",");
    }

    public static Set<String> arrayToSet(String[] tags) {
        if (tags == null) {
            return null;
        }
        Set<String> tagSet = new HashSet<String>();
        for (int i = 0; i < tags.length; i++) {
            tagSet.add(tags[i].trim());
        }
        return tagSet;
    }

    public static String[] list2Array(List<String> list) {
        if (list == null || list.isEmpty())
            return null;
        String[] str = new String[list.size()];
        return list.toArray(str);
    }

    public static List<String> array2List(String[] strs) {
        List<String> lis = new ArrayList<String>();
        if (strs == null) {
            return lis;
        }

        for (int i = 0; i < strs.length; i++) {
            lis.add(strs[i]);
        }
        return lis;
    }

    public static List<String> checkLabelValid(List<String> likedActLabel) {
        String label = likedActLabel.get(0);
        if (label == null || TextUtils.isEmpty(label.trim()) || StringUtil.isEmpty(label)) {
            return null;
        } else {
            return likedActLabel;
        }
    }

    public static boolean isPhoneMatcher(String phone) {
        Pattern pattern = Pattern.compile("^((13[0-9])|(15[^4,\\D])|(18[0,5-9]))\\d{8}$");
        Matcher matcher = pattern.matcher(phone);
        return matcher.matches();
    }

    public static boolean isPasswordVaild(String password) {
        if (password.length() < 6) {
            return false;
        }


        return true;
    }
	
}
